package offer;

public class offerII_0036 {
    public int evalRPN(String[] tokens) {
        int len=tokens.length;
        int [] stack=new int[len];
        int current=0;
        for(int i=0;i<len;++i){
            if("+".equals(tokens[i])){
                stack[current-2]=stack[current-2]+stack[current-1];
                current--;
            }
            else if("-".equals(tokens[i])){
                stack[current-2]=stack[current-2]-stack[current-1];
                current--;
            }
            else if("*".equals(tokens[i])){
                stack[current-2]=stack[current-2]*stack[current-1];
                current--;
            }
            else if("/".equals(tokens[i])){
                stack[current-2]=stack[current-2]/stack[current-1];
                current--;
            }
            else{
                int len1=tokens[i].length();
                int sum=0;
                int j=0;
                int flag=1;
                if(tokens[i].charAt(0)=='-') {flag=-1;j++;}
                for(;j<len1;++j){
                    sum=10*sum+(tokens[i].charAt(j)-'0');
                }
                stack[current]=sum*flag;
                current++;
            }
        }
        return stack[0];
    }
}
